// map-test原生测试页面脚本
Page({
  data: {
    logs: [],
    hasLocation: false,
    location: {}
  },
  
  onLoad: function() {
    this.addLog('页面加载完成');
    this.addLog('当前API权限配置:');
    this.addLog(JSON.stringify({
      'requiredPrivateInfos': [
        'getLocation',
        'chooseLocation',
        'startLocationUpdate',
        'startLocationUpdateBackground',
        'onLocationChange'
      ]
    }));
  },
  
  // 添加日志
  addLog: function(text) {
    const logs = this.data.logs;
    logs.unshift({
      text: text,
      time: new Date().toLocaleTimeString()
    });
    this.setData({
      logs: logs
    });
    console.log(text);
  },
  
  // 获取位置信息
  getLocation: function() {
    const that = this;
    
    // 先检查是否授权
    this.addLog('检查位置权限');
    wx.getSetting({
      success(res) {
        that.addLog('获取设置成功: ' + JSON.stringify(res.authSetting));
        
        if (res.authSetting['scope.userLocation'] !== true) {
          that.addLog('未授权位置信息，尝试获取授权');
          
          // 未授权则请求授权
          wx.authorize({
            scope: 'scope.userLocation',
            success() {
              that.addLog('授权成功，开始获取位置');
              that.startGetLocation();
            },
            fail(err) {
              that.addLog('授权失败: ' + JSON.stringify(err));
              // 打开设置页面让用户手动授权
              wx.showModal({
                title: '提示',
                content: '需要您的位置权限才能使用导航功能',
                confirmText: '去设置',
                success(res) {
                  if (res.confirm) {
                    wx.openSetting({
                      success(res) {
                        if (res.authSetting['scope.userLocation']) {
                          that.addLog('设置授权成功，开始获取位置');
                          that.startGetLocation();
                        } else {
                          that.addLog('用户拒绝授权');
                        }
                      },
                      fail(err) {
                        that.addLog('打开设置页面失败: ' + JSON.stringify(err));
                      }
                    });
                  } else {
                    that.addLog('用户取消设置');
                  }
                }
              });
            }
          });
        } else {
          that.addLog('已授权位置信息，开始获取位置');
          that.startGetLocation();
        }
      },
      fail(err) {
        that.addLog('获取设置失败: ' + JSON.stringify(err));
      }
    });
  },
  
  // 开始获取位置
  startGetLocation: function() {
    const that = this;
    
    this.addLog('调用wx.getLocation API');
    wx.getLocation({
      type: 'gcj02',
      success(res) {
        that.addLog('获取位置成功: ' + JSON.stringify(res));
        that.setData({
          hasLocation: true,
          location: {
            latitude: res.latitude,
            longitude: res.longitude,
            speed: res.speed,
            accuracy: res.accuracy
          }
        });
      },
      fail(err) {
        that.addLog('获取位置失败: ' + JSON.stringify(err));
      },
      complete() {
        that.addLog('位置请求完成');
      }
    });
  },
  
  // 使用微信内置地图查看位置
  openLocation: function() {
    const that = this;
    
    if (!this.data.hasLocation) {
      this.addLog('没有位置信息，请先获取位置');
      return;
    }
    
    this.addLog('打开内置地图');
    wx.openLocation({
      latitude: this.data.location.latitude,
      longitude: this.data.location.longitude,
      scale: 18,
      name: '当前位置',
      address: '位置信息由小程序获取',
      success() {
        that.addLog('打开地图成功');
      },
      fail(err) {
        that.addLog('打开地图失败: ' + JSON.stringify(err));
      }
    });
  },
  
  // 清除日志
  clearLogs: function() {
    this.setData({
      logs: []
    });
  }
}); 